package com.ray.controller;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.ProcedureBean;
import com.ray.bean.ProcessBean;
import com.ray.bean.TableData;
import com.ray.exception.ServiceException;
import com.ray.pojo.MyUser;
import com.ray.service.ProcedureService;
import com.ray.util.ExcelExportUtil;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;
import com.ray.util.ReturnStringUtil;

@Controller
@RequestMapping("procedure")
public class ProcedureController {

	@Autowired
	ProcedureService procedureService;
	
	//上传工序表
	@RequestMapping(value="/addProcessByFile",method=RequestMethod.POST)
	@ResponseBody
	public Map addProcessByFile(MultipartFile filename,HttpServletRequest request) throws Exception {
		MyUser myUser=RequestUtil.getLoginUser(request);
		return ResponseUtil.ResponseResult(procedureService.addProcessByFile(filename,myUser));
	}
	//下载工序表
	@RequestMapping(value="/downloadProcedure",method=RequestMethod.GET)
	@ResponseBody
	public void downloadProcedure(HttpServletRequest request,HttpServletResponse response) throws ServiceException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, IOException {
		Map map=RequestUtil.getRequestAllParam(request);
		//获取表数据
		List<ProcessBean> tableList = procedureService.getTableDate(map);
		//表头字段名
		String [] tableHeadFields={"工序代码","部门","车间名称","工序名称","工序作业时间(非负整数)","顺序(非负整数)","状态(使用/禁用)","单价(非负浮点数)","计价单位"};
		//数据字段名
		String [] fields={"processCode","productionLine","workshopName","processName","workingProcedureDuration","sort","status","unitPrice","chargeUnit"};
		//临时文件夹
		String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
		//创建excel文件
		File excelFile=ExcelExportUtil.createExcelFile(tableList,tempFolder,tableHeadFields,fields);
		//下载并删除本地文件
		ResponseUtil.downloadfile(response, excelFile, "工序表.xlsx", true);
	}
   //添加工序
	@RequestMapping("/addProcedur")
	@ResponseBody
	public Map addProcedur(ProcedureBean procedureBean) {
		procedureService.addProcedur(procedureBean);
		return ResponseUtil.ResponseResult(ReturnStringUtil.ADD_SUCCESS);
	}
	
	 //获取工序详情
	@RequestMapping("/getProcessDetail")
	@ResponseBody
	public Map getProcessDetail(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(procedureService.getProcessDetail(map));
	}
	
	//添加工序 wangr
	@RequestMapping("/insertProcess")
	@ResponseBody
	public Map insertProcess(ProcessBean processBean,HttpServletRequest request) throws ServiceException {
		Map<String,Object> map=RequestUtil.getRequestParamByGeneric(request);
		procedureService.insertProcess(processBean,map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.ADD_SUCCESS);
	}
	
	/**
	 * 通过id更新工序
	 * @param processBean
	 * @param request
	 * @throws ServiceException
	 * @author wangrong
	 */
	@RequestMapping("/updateProcessByCode")
	@ResponseBody
	public Map<String,Object> updateProcessByCode(ProcessBean processBean,HttpServletRequest request) throws ServiceException {
		Map<String,Object> map=RequestUtil.getRequestParamByGeneric(request);
		procedureService.updateProcessByCode(processBean,map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.UPDATE_SUCCESS);
	}
	
	
	@RequestMapping("/updateAllProcessByCode")
	@ResponseBody
	public Map<String,Object> updateAllProcessByCode(String beanArray,HttpServletRequest request) throws ServiceException {
		Map<String,Object> map=RequestUtil.getRequestParamByGeneric(request);
		procedureService.updateAllProcessByCode(beanArray,map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.UPDATE_SUCCESS);
	}
	
	@RequestMapping("/deleteAllProcess")
	@ResponseBody
	public Map deleteAllProcess(String idArray) throws ServiceException {
		procedureService.deleteAllProcess(idArray);
		return ResponseUtil.ResponseResult(ReturnStringUtil.DELETE_SUCCESS);
	}
	
	
	
	/*//修改工序 wangr
	@RequestMapping("/updateProcess")
	@ResponseBody
	public Map updateProcess(ProcessBean processBean,HttpServletRequest request) throws ServiceException {
		MyUser myuser=RequestUtil.getLoginUser(request);
		procedureService.updateProcess(processBean,myuser);
		return ResponseUtil.ResponseResult(ReturnStringUtil.UPDATE_SUCCESS);
	}*/
	
//	从工序表获取所有生产线
	@RequestMapping("/getProductionLineFromProcess")
	@ResponseBody
	public Map getProductionLineFromProcess(){
		return ResponseUtil.ResponseResult(procedureService.getProductionLineFromProcess());
	}
	
	@RequestMapping("/getAllProcess")
	@ResponseBody
	public Map getAllProcess(HttpServletRequest request) throws ServiceException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(procedureService.getAllProcess(map));
	}
	
	/**
	 * 设置工序qc表
	 * @param processId 工序id
	 * @param QCDetailArray qc详情配置
	 * selfCheckTime 自检时间
	 * siteCheckTime 巡检时间
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("/setProcessQCTable")
	@ResponseBody
	public Map setProcessQCTable(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(procedureService.setProcessQCTable(map));
	}
	
	/**
	 * 获取工序qc表
	 * @param processId 工序id
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("/getProcessQCTableByProcessId")
	@ResponseBody
	public Map getProcessQCTableByProcessId(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(procedureService.getProcessQCTableByProcessId(map));
	}
	
	
	/**
	 * 获取工序qc表
	 * jsonObject 传参数组
	 * 格式{
	 *     orderId:订单编号，
	 *     processId：工序id,
	 *     type:类型 1首检   2自检  3巡检
	 *     detail:[
	 *         {
	 *            content:内容,
	 *            rowIndex:行号，
	 *            result：1是  2 否，
	 *            reportValues:值
	 *         }
	 *     ]
	 * }
	 * @param processId 工序id
	 * @return
	 * @throws ParseException 
	 * @throws ServiceException
	 */
	@RequestMapping("/qcReport")
	@ResponseBody
	public Map qcReport(HttpServletRequest request) throws ParseException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(procedureService.qcReport(map));
	}
	
	
	/**
	 * 获取qc报工内容
	 * companyOrderId 公司订单编号
	 * factoryId 工厂型号
	 * customerId 客户型号
	 * processId 工序id
	 * startTime 开始时间
	 * endTime 结束时间
	 * status 状态
	 * result 通过不通过
	 * @param request
	 * @return
	 */
	@RequestMapping("/getQcReportDataSelf")
	@ResponseBody
	public Map getQcReportDataSelf(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(procedureService.getQcReportDataSelf(map));
	}
	
	/**
	 * 获取qc报工内容
	 * companyOrderId 公司订单编号
	 * factoryId 工厂型号
	 * customerId 客户型号
	 * processId 工序id
	 * createUser qc人
	 * startTime 开始时间
	 * endTime 结束时间
	 * status 状态
	 * result 通过不通过
	 * @param request
	 * @return
	 */
	@RequestMapping("/getQcReportDataApproval")
	@ResponseBody
	public Map getQcReportDataApproval(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(procedureService.getQcReportDataApproval(map));
	}
	
	
	/**
	 * 获取qc报工内容
	 * idArray 
	 * status 1通过 2 不通过
	 * @param request
	 * @return
	 */
	@RequestMapping("/approvalQCReport")
	@ResponseBody
	public Map approvalQCReport(HttpServletRequest request) {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(procedureService.approvalQCReport(map));
	}
	
	
	
	
}
